package com.aiyige.page.my.buy;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.model.enums.GoodsType;
import com.aiyige.track.annotation.Track;
import com.aiyige.utils.widget.MyEditText;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiachao on 2017/11/16.
 */
@Track("我的学习")
@Route(path = ARouterConfig.MyBuyPage,extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class MyBuyPage extends BaseActivity implements MyBuyDataPage.Callback{

    @BindView(R.id.mainVp)
    ViewPager mainVp;
    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.et_search)
    MyEditText myEditText;

    MyPagerAdapter adapter;

    public static void start() {
        ARouter.getInstance().build(ARouterConfig.MyBuyPage).navigation();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_my_buy);
        ButterKnife.bind(this);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        mainVp.setAdapter(adapter);
        mainVp.setOffscreenPageLimit(adapter.pageList.size());
        tabLayout.setViewPager(mainVp);
        myEditText.setActionSearch(new MyEditText.ActionCallback() {
            @Override
            public void actionSearch(String value) {
                doSearch();
            }
        });
        myEditText.setEditTextHint(getString(R.string.search));

    }



    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public String getSearchValue() {
        return myEditText.getValue().trim();
    }

    private void doSearch() {
        if (isEmpty(getSearchValue())) {
            showToast(getString(R.string.can_not_empty));
            return;
        }
        SearchMyBuyPage.start(getSearchValue());
//        if (adapter == null || adapter.pageList == null
//                || adapter.pageList.size() == 0) {
//            return;
//        }
//        for (MyBuyDataPage fragment : adapter.pageList) {
//            fragment.refresh();
//        }
    }


    public class MyPagerAdapter extends FragmentPagerAdapter {
        List<MyBuyDataPage> pageList = new LinkedList<>();



        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
            pageList.add(MyBuyDataPage.newInstance(GoodsType.TYPE_VIDEO_COURSE.getType(),MyBuyPage.this));
            pageList.add(MyBuyDataPage.newInstance(GoodsType.TYPE_MAJOR_COURSE.getType(),MyBuyPage.this));
//            pageList.add(MyBuyDataPage.newInstance(GoodsType.TYPE_TRAINING_COURSE.getType(),MyBuyPage.this));
//            pageList.add(MyBuyDataPage.newInstance(GoodsType.TYPE_PRIVATE_COURSE.getType(),MyBuyPage.this));
//            pageList.add(MyBuyDataPage.newInstance(GoodsType.TYPE_COMMON_GOODS.getType(),MyBuyPage.this));
        }

        @Override
        public Fragment getItem(int position) {
            return pageList.get(position);
        }

        @Override
        public int getCount() {
            return pageList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return pageList.get(position).getPageTitle();
        }
    }
}
